package com.ygqh.baby.service.pay.app;

import com.alibaba.fastjson.JSONObject;
import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.CodeType;
import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.observer.Observable.OrderPaySuccessObservable;
import com.ygqh.baby.observer.OrderPayModel;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.service.YgOrderLogService;
import com.ygqh.baby.service.YgOrderPaymentService;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.service.YgRechargeMemberRecordService;
import com.ygqh.baby.utils.DateConvertUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

@Component
public class AppPaySuccessHandler {
	private Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private YgOrderService ygOrderService;
	@Autowired
	private YgOrderLogService ygOrderLogService;
	@Autowired
	private YgRechargeMemberRecordService ygRechargeMemberRecordService;
	@Autowired
	private YgOrderPaymentService ygOrderPaymentService;
	@Autowired
	private OrderPaySuccessObservable orderPaySuccessObservable;

	public void paySuccess(PayType payType, String paymentCode, String transactionId, BigDecimal paidPrice, Date paidTime, JSONObject params) {

		try {
			AppType appType = AppType.valueOf((String) params.get("appType"));
			CodeType codeType = CodeType.valueOf((String) params.get("codeType"));

			ygOrderLogService.addOrderLog(Long.parseLong(paymentCode), OrderStatus.WaitingPay, appType + ":微信支付回调",
					"接收到微信支付回调，这里的orderId是支付流水号。transactionId=" + transactionId);
			if (CodeType.OrderPM.equals(codeType)) {
				// 订单支付成功处理业务逻辑
				orderPaySuccess(paymentCode, transactionId, appType, payType, paidPrice, paidTime);
			} else if (CodeType.RechargeMB.equals(codeType) || CodeType.RechargeAG.equals(codeType)) {
				Long userId = Long.valueOf(params.get("userId").toString());
				ygRechargeMemberRecordService.doPaySuccessBusiness(Long.parseLong(paymentCode), paidPrice, userId, transactionId);
			} else {
				logger.warn("没有该类型的充值业务；" + codeType);
			}
		} catch (Exception e) {
			logger.error("订单支付成功，处理业务逻辑失败。===paymentCode :" + paymentCode + ";" + e.getMessage());
			ygOrderLogService.addOrderLog(Long.parseLong(paymentCode), OrderStatus.WaitingSend, "系统",
					"订单支付成功后，处理业务逻辑失败;支付流水号：" + paymentCode + ";" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void orderPaySuccess(String paymentCode, String tradeNo, AppType appType, PayType payType, BigDecimal paidPrice, Date paidTime) {

		// 支付成功
		YgOrder order = ygOrderPaymentService.findOrderByPaymentCode(paymentCode, false);
		if (order == null) {
			logger.warn("alipay支付回调：没有支付流水号=" + paymentCode + "的未支付订单。tradeNo=" + tradeNo + ";PayType=" + payType);
		} else {

			int row = ygOrderPaymentService.updatePaySuccess(paymentCode, tradeNo, appType, payType, paidPrice, paidTime);
			if (row > 0) {
				ygOrderService.splitOrderV3(order.getOrderId(), paidPrice, payType, paidTime);
			}
			orderPaySuccessObservable.paySuccess(new OrderPayModel(order.getOrderId(), paidPrice, payType, paymentCode));
		}

	}

	@SuppressWarnings("unused")
	private void doVipOldBusiness(JSONObject params, String paymentCode, String transactionId, BigDecimal totalPrice) {
		Long userId = (Long) params.get("userId");
		String cardIds = (String) params.get("cardIds");
		String birthday = (String) params.get("birthday");
		Date date = DateConvertUtils.parse(birthday, DateConvertUtils.DATE_TIME_FORMAT);
		ygRechargeMemberRecordService.doPaySuccessBusiness(Long.parseLong(paymentCode), totalPrice, userId, transactionId, cardIds, date);

	}
}
